/**
 * @class
 * @description 设置阅览器章节URL
 * @constructor
 */
import BaseContext from '../context';
export default class HistoryURL extends BaseContext {
  // 初始化操作
  init() {
    this.listener({
      chapterIndexChange: this.replaceState.bind(this)
    })
  }

  // 替换当前浏览域名
  replaceState() {
    const { chapterSEO, chapterId} = this.currentChapter;
    document.head.title = chapterSEO.title;
    const herf = location.href;
    const reg = /\/(\d+).html/;
    const match = herf.match(reg);
    // 处理url
    if (!history.replaceState || (match && parseInt(match[1], 10) === chapterId)) {
      return;
    }
    const href = herf.replace(reg, `/${chapterId}.html`);
    history.replaceState({}, '漫画阅读', href);
  }
}
